package cn.edu.pzhu.javaweb.dao.imp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import cn.edu.pzhu.javaweb.dao.UserDao;
import cn.edu.pzhu.javaweb.pojo.User;
import cn.edu.pzhu.javaweb.util.DBUtil;

public class UserDaoImp implements UserDao {

	@Override
	public int insert(User entity) {
		Connection con = null;
		try {
			con = DBUtil.getConnection();
			return insert(entity, con);
		} catch (Exception e) {
			e.printStackTrace();
			return 0;
		} finally {
			DBUtil.closeConnection(con);
		}
	}

	@Override
	public int deleteById(String id) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int deleteBatchIds(List<String> ids) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public int updateById(User entity) {
		Connection con = null;
		PreparedStatement pstm = null;
		int count = 0;
		try {
			// 1. 获取数据库连接
			con = DBUtil.getConnection();
			
			// 2. 编写SQL语句，更新用户信息
			String sql = "update user set password=?, status=? where username=?";
			
			// 3. 创建预处理语句
			pstm = con.prepareStatement(sql);
			
			// 4. 设置参数
			pstm.setString(1, entity.getPassword());
			pstm.setInt(2, entity.getStatus() != null ? entity.getStatus() : 1); // 默认状态为1
			pstm.setString(3, entity.getUsername());
			
			// 5. 执行更新
			count = pstm.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 6. 关闭资源
			DBUtil.closeStatement(pstm);
			DBUtil.closeConnection(con);
		}
		return count;
	}
	
	@Override
	public int insert(User entity, Connection con) {
		PreparedStatement pstm = null;
		int count = 0;
		try {
			// 1. 编写SQL语句，插入用户信息
			String sql = "insert into user(username, password, status) values(?, ?, ?)";
			
			// 2. 创建预处理语句
			pstm = con.prepareStatement(sql);
			
			// 3. 设置参数
			pstm.setString(1, entity.getUsername());
			pstm.setString(2, entity.getPassword());
			pstm.setInt(3, entity.getStatus() != null ? entity.getStatus() : 1); // 默认状态为1
			
			// 4. 执行插入
			count = pstm.executeUpdate();
			
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			// 5. 关闭资源
			DBUtil.closeStatement(pstm);
			// 注意：这里不关闭connection，因为它是传入的参数，由调用方负责关闭
		}
		return count;
	}

	@Override
	public User selectById(String id) {
		// TODO Auto-generated method stub
	    Connection con = null;
	    PreparedStatement pstm = null;
	    ResultSet res = null;
	    User user = null;
	    
	    try {
	        // 1. 获取数据库连接
	        con = DBUtil.getConnection();
	        
	        // 2. 准备SQL语句（使用占位符防止SQL注入）
	        String sql = "select * from user where username=?";
	        pstm = con.prepareStatement(sql);
	        
	        // 3. 设置参数
	        pstm.setString(1, id);
	        
	        // 4. 执行查询
	        res = pstm.executeQuery();
	        
	        // 5. 处理结果集
	        if (res.next()) {
	            user = new User();
	            user.setUsername(res.getString(1));
	            user.setPassword(res.getString(2));
	            user.setStatus(res.getInt(3));
	        }
	        
	        return user;
	        
	    } catch (SQLException e) {
	        System.err.println("查询用户信息失败，用户名：" + id);
	        e.printStackTrace();
	        return null;
	    } finally {
	        // 6. 释放资源
	        DBUtil.closeResources(res, pstm, con);
	    }
	}

	@Override
	public List<User> selectBatchIds(List<String> ids) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> selectAll() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> selectByCondition(User condition) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long selectCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public long selectCountByCondition(User condition) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<User> selectPage(int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<User> selectPageByCondition(User condition, int pageNum, int pageSize) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public boolean existsById(String id) {
		// TODO Auto-generated method stub
		return false;
	}
}
